define(function () {
  /**
   * 多边形标绘
   * @param {Cesium.Viewer} viewer 基本视图
   * @param {Cesium.ScreenSpaceEventHandler} handler 屏幕监听事件
   * @param {Cesium.CustomDataSource}plotEntities 标绘实体集合
   */
  let polygonPlotting = function (viewer, handler, plotEntities) {
    let activeShapePoints = []                       //线段端点集合

    /**
     *添加多边形实体
     *@param {Cesium.Cartesian3 []} positionData 线段数组
     */
    function drawShape(positionData) {
      return plotEntities.entities.add({
        description: 'plot',
        polygon: {
          hierarchy: positionData,
          material: new Cesium.ColorMaterialProperty(
            Cesium.Color.WHITE.withAlpha(0.4)
          ),
        },
      })
    }

    /**
     *添加点
     *@param {Cesium.Cartesian3} worldPosition
     */
    function createPoint(worldPosition) {
      return plotEntities.entities.add({
        description: 'plot',
        position: worldPosition,
        point: {
          color: Cesium.Color.WHITE,
          pixelSize: 5,
        },
      })
    }

    //左键点下，绘画点与图形
    handler.setInputAction((event) => {
      let earthPosition = viewer.camera.pickEllipsoid(event.position, viewer.scene.globe.ellipsoid)
      if (activeShapePoints.length === 0) {
        createPoint(earthPosition)
      }
      activeShapePoints.push(earthPosition)
      drawShape(activeShapePoints)
      createPoint(earthPosition)
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK)

    //右键点下，初始化
    handler.setInputAction(function (event) {
      activeShapePoints = []
    }, Cesium.ScreenSpaceEventType.RIGHT_CLICK)
  }
  return polygonPlotting
})